package com.IT.servletJsp.medicine;

import com.IT.service.AdminFunctionService;
import com.IT.service.Impl.AdminFunctionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "/admin/deleteMedicine")
public class deleteMedicineJsp extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset = utf-8");

        PrintWriter printWriter = response.getWriter();
        String mName = request.getParameter("mName");

        AdminFunctionService adminFunctionService = new AdminFunctionServiceImpl();
        int flag = adminFunctionService.deleteMedicine(mName);

        if(flag == 1){
            //response.sendRedirect("/adminInterface.html");
            //删除成功
            printWriter.print("<script>alert('删除成功！');window.location.href='/adminInterface.html'</script>");
        }
        else if(flag == -1){
            //药品不存在
            printWriter.print("<script>alert('药品不存在！');window.location.href='/deleteMedicine.html'</script>");
        }
        else {
            //flag == 0;数据库操作异常
            printWriter.print("<script>alert('数据库操作异常！');window.location.href='/deleteMedicine.html'</script>");
        }
    }
}
